package top.mhtools.pigeon.common.exception;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletRequest;

/**
 * 全局异常捕获
 *
 * @author machen
 * @date 2020/4/24 15:03
 */
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(value = {ServiceException.class})
    public Result serviceException(HttpServletRequest request, ServiceException ex) {
        if (ex.getCause() != null) {
            // 其他异常引起的ServiceException
            log.error("[{}] {} [ex] {}", request.getMethod(), request.getRequestURL().toString(), ex.toString(),
                    ex.getCause());
        } else {
            // 业务异常
            log.error("[{}] {} [ex] {}", request.getMethod(), request.getRequestURL().toString(), ex.toString());
        }

        return Results.failure(ex);
    }
}
